import pygame
from settings import *
from game import Game
from ui import draw_text, button

# 初始化pygame
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("assets/sounds/ConcernedApe - Summer(Nature's Crescendo).flac")  # 添加背景音乐文件
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)
# 创建游戏窗口
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Mosquito Exterminator")

# 加载背景图片
try:
    background = pygame.image.load("assets/background.jpg")  # 替换为你的背景图片路径
    background = pygame.transform.scale(background, (SCREEN_WIDTH, SCREEN_HEIGHT))
except:
    print("Warning: Could not load background image")
    background = None

# 创建游戏实例
game = Game(screen)

# 游戏状态
MENU = 0
SINGLE_PLAYER = 1
TWO_PLAYERS = 2
current_state = MENU

# 游戏主循环
running = True
# 在 main.py 中修改游戏循环部分

while running:
    if background:
        screen.blit(background, (0, 0))
    else:
        screen.fill((240, 240, 240))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    if current_state == MENU:
        menu_panel = pygame.Surface((400, 350), pygame.SRCALPHA)
        menu_panel.fill((0, 0, 0, 128))
        screen.blit(menu_panel, (SCREEN_WIDTH//2 - 200, 100))

        draw_text(screen, "Mosquito Exterminator", (SCREEN_WIDTH // 2, 150),
                 (255, 255, 255), FONTS["medium"], shadow=True, center=True)

        if button(screen, SCREEN_WIDTH // 2 - 100, "One Player", y_offset=200):
            current_state = SINGLE_PLAYER
            game.is_two_player = False
            game.reset()

        if button(screen, SCREEN_WIDTH // 2 - 100, "Two Players", y_offset=280):
            current_state = TWO_PLAYERS
            game.is_two_player = True
            game.reset()

    elif current_state in (SINGLE_PLAYER, TWO_PLAYERS):
        result = game.update()
        if result == "menu":
            current_state = MENU

    pygame.display.update()